<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-form-item label="设备名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="厂商" prop="from">
        <el-input v-model="dataForm.from" placeholder="厂商"></el-input>
      </el-form-item>
      <el-form-item label="型号" prop="type">
        <el-input v-model="dataForm.type" placeholder="型号"></el-input>
      </el-form-item>
      <el-form-item label="序列号" prop="serialNumber">
        <el-input v-model="dataForm.serialNumber" placeholder="序列号"></el-input>
      </el-form-item>
      <el-form-item label="支持的功能" prop="function">
        <el-input v-model="dataForm.function" placeholder="支持的功能"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getPvmDeviceObj, addPvmDeviceObj, putPvmDeviceObj, getList} from '@/api/pvm/pvmdevice'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        deviceType: '',
        from: '',
        vendorId: '',
        type: '',
        typeId: '',
        serialNumber: '',
        function: '',
        createTime: '',
        updateTime: '',
        createUser: '',
        updateUser: '',
        unitId: '',
        tenantId: '',
        delFlag: ''
      },
      deviceList:{},
      vendorList:{},
      typeList:{},
      dataRule: {
        name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        deviceType: [
          { required: true, message: '设备类型id不能为空', trigger: 'blur' }
        ],
        from: [
          { required: true, message: '设备厂商不能为空', trigger: 'blur' }
        ],
        vendorId: [
          { required: true, message: '厂商id不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '型号不能为空', trigger: 'blur' }
        ],
        typeId: [
          { required: true, message: '型号id不能为空', trigger: 'blur' }
        ],
        // serialNumber: [
        //   { required: true, message: '序列号不能为空', trigger: 'blur' }
        // ],
        function: [
          { required: true, message: '支持的功能不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.getDeviceList()
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getPvmDeviceObj(this.dataForm.id).then(response => {
            if(response.data.code == '0'){
              this.dataForm = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
          this.deviceTypeChange()
          this.vendorChange()
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            putPvmDeviceObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
            })
          } else {
            addPvmDeviceObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        }
      })
    },
    getDeviceList(){
      getList('device',this.deviceList).then(response =>{
        if(response.data.code == '0'){
          this.deviceList=response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    deviceTypeChange(){
      getList('vendor',this.dataForm).then(response =>{
        if(response.data.code == '0'){
          this.vendorList = response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    vendorChange(){
      getList('type',this.dataForm).then(response =>{
        if(response.data.code == '0'){
          this.typeList = response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
  }
}
</script>
